<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/12/18
 * Time: 11:45
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Xl extends Base
{
    /**
     * 系列列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $where['device_type'] = 1;
            $where['device_status'] = ['BETWEEN', [1, 2]];
            $device_name = input('name'); // 系列名称

            // 关键字
            if(isset($device_name) && $device_name != ''){
                $where['device_name'] = ['like', '%' . $device_name . '%'];
            }

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('xl')->where($where)->count();// 获取总条数
            $lists = Db::name('xl')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('device_status asc, device_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 新增系列
     * @return mixed|\think\response\Json
     */
    public function xlAdd(){
        if(Request::instance()->isPost()){
            $xl_name = input('xl_name');

            $insert = [
                'device_name' => $xl_name,
                'device_type' => 1,
                'device_time' => createTime()
            ];
            $res = Db::table('up_xl')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        return $this->fetch();
    }

    /**
     * 更新系列
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function xlUpdate(){
        $xl_id = input('xl_id');
        if(Request::instance()->isPost()){
            $xl_name = input('xl_name');

            $update = [
                'device_id' => $xl_id,
                'device_name' => $xl_name,
                'device_update_time' => createTime()
            ];
            $res = Db::table('up_xl')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $xl = Db::table('up_xl')->where('device_id', $xl_id)->field('device_name')->find();
        return $this->fetch('', ['xl_id' => $xl_id, 'xl' => $xl]);
    }

    /**
     * 上架/下架系列
     * @return \think\response\Json
     */
    public function xlStatus(){
        $xl_id = input('id');
        $status = Db::table('up_xl')->where('device_id', $xl_id)->value('device_status');
        $newStatus = $status == 1 ? 2 : 1;

        $res = Db::table('up_xl')->where('device_id', $xl_id)->setField('device_status', $newStatus);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 删除系列
     * @return \think\response\Json
     */
    public function xlDel(){
        $xl_id = input('id');

        $res = Db::table('up_xl')->where('device_id', $xl_id)->setField('device_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 型号列表
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function xh(){
        if(request()->isAjax()){
            $where['device_pid'] = session('xl_id');
            $where['device_status'] = ['BETWEEN', [1, 2]];
            $device_name = input('name'); // 型号名称

            // 关键字
            if(isset($keyword) && $keyword != ''){
                $where['device_name'] = ['like', '%' . $device_name . '%'];
            }

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('xl')->where($where)->count();// 获取总条数
            $lists = Db::name('xl')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('device_status asc, device_time desc')
                ->select();
            if($lists){
                $gz = Db::table('up_gz')->where('gz_status', 1)->field('gz_id, gz_name')->select();
                foreach ($lists as $k => $v){
                    $gzStr = '';
                    if($v['device_gz']){
                        foreach ($gz as $kg => $vg){
                            $gzArr = explode(',', $v['device_gz']);
                            if(in_array($vg['gz_id'], $gzArr)){
                                $gzStr .= $vg['gz_name'] . '/';
                            }
                        }
                        $gzStr = trim($gzStr, '/');
                    }
                    $lists[$k]['gz'] = $gzStr;
                }
            }

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $xl_id = input('xl_id');
        if($xl_id) session('xl_id', $xl_id);
        return $this->fetch();
    }

    /**
     * 新增型号
     * @return mixed|\think\response\Json
     */
    public function xhAdd(){
        if(Request::instance()->isPost()){
            $xh_name = input('xh_name');
            $device_gz = input('device_gz');

            $insert = [
                'device_name' => $xh_name,
                'device_gz' => $device_gz,
                'device_type' => 2,
                'device_pid' => session('xl_id'),
                'device_time' => createTime()
            ];
            $res = Db::table('up_xl')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $gz = Db::table('up_gz')->where('gz_status', 1)->field('gz_id, gz_name')->select();
        return $this->fetch('', ['gz' => $gz]);
    }

    /**
     * 更新型号
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function xhUpdate(){
        $xh_id = input('xh_id');
        if(Request::instance()->isPost()){
            $xh_name = input('xh_name');
            $device_gz = input('device_gz');

            $update = [
                'device_id' => $xh_id,
                'device_gz' => $device_gz,
                'device_name' => $xh_name,
                'device_update_time' => createTime()
            ];
            $res = Db::table('up_xl')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $xh = Db::table('up_xl')->where('device_id', $xh_id)->field('device_name, device_gz')->find();
        $gz = Db::table('up_gz')->where('gz_status', 1)->field('gz_id, gz_name')->select();
        return $this->fetch('', ['xh_id' => $xh_id, 'xh' => $xh, 'gz' => $gz]);
    }

    /**
     * 上架/下架型号
     * @return \think\response\Json
     */
    public function xhStatus(){
        $xh_id = input('id');
        $status = Db::table('up_xl')->where('device_id', $xh_id)->value('device_status');
        $newStatus = $status == 1 ? 2 : 1;

        $res = Db::table('up_xl')->where('device_id', $xh_id)->setField('device_status', $newStatus);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 删除型号
     * @return \think\response\Json
     */
    public function xhDel(){
        $xh_id = input('id');

        $res = Db::table('up_xl')->where('device_id', $xh_id)->setField('device_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}